<template>
	<view class="container">
		<!-- 头部 -->
		<view class="top">
			<text class="city" @click="chosecity">{{now_city}}</text>
			<view class="searchbox" url="../search/search">
				<image src="../../static/assets/search.png" mode=""></image>
				<input type="text" v-model="iptval" placeholder="请输入区域/小区,搜索房源"
				 @keydown.enter="getsearch"/>
			</view>
			<text class="cancle" @click="cancle">取消</text>
		</view>
		<!-- 搜索结果 -->
		<view class="title">
			<text>搜索结果</text>
		</view>
		<view class="resultList" v-if="isResult">
			<block v-for="item in resultList" v-bind:key="item.house_id">
				<navigator :url="'../houseDetail/houseDetail?id='+encodeURIComponent(item.house_id)" class="item">
					<view class="pic">
						<image :src="item.house_main_image" mode=""></image>
					</view>
					<view class="content">
						<text class="title">{{item.house_title}}</text>
						<text class="area">{{item.house_area_desc}}</text>
					</view>
				</navigator>
			</block>
		</view>
		<text class="nothing" v-if="isNothing">没有搜索到你想要的内容</text>
		<!-- 历史记录 -->
		<view class="title">
			<text>历史记录</text>
			<image @click="deleteHistory" src="../../static/assets/delete.png" mode=""></image>
		</view>
		<view class="historylist">
			<block v-for="item in searchwords">
				<view class="item">{{item}}</view>
			</block>
		</view>
		<!-- 热门搜索 -->
		<view class="title">
			<text>热门搜索</text>
			<image src="../../static/assets/location.png" mode=""></image>
		</view>
		<view class="hotlist">
			<block v-for="(item,index) in hotword" v-bind:key="item.id">
				<text class="hotitem" @click="clicktoSearch(item.text)">{{item.text}}</text>
			</block>
		</view>
		
	</view>
</template>

<script>
	import store from "../../store/index.js";
	import {cityHouseList} from "../../data/cityhouses.js";
	import {houseDetail} from "../../data/housedetail.js";//北京详情列表
	export default {
		computed:{
			now_city(){
				return store.state.nowCity;
			}	
		},
		data() {
			return {
				iptval:'',
				citycode:0,//城市code
				cityHouseList:[],//城市数据列表
				resultList:[],//搜索结果列表
				isNothing:false,
				isResult:true,
				searchwords:[],
				hotword:[
					{text:'大兴',id:0},
					{text:'西湖',id:1},
					{text:'天河',id:2},
					{text:'故宫',id:3},
				]
			}
		},
		methods: {
			cancle(){//点击取消返回首页
				// uni.switchTab({//跳转至navtab下的页面
				// 	url:"../index/index"
				// })
				uni.navigateBack();
			},
			chosecity(){
				uni.navigateTo({
					url:'../choosecity/choosecity',
					animationType:'slide-in-right',
					animationDuration:3000
				})
			},
			getCity(){//选择城市后得到城市列表
				console.log(this.now_city);
				if(this.now_city=='北京'){
					this.citycode=1;
				}
				else if(this.now_city=='广州'){
					this.citycode=0;
				}
				else if(this.now_city=='杭州'){
					this.citycode=2;
				}
				else if(this.now_city=='南京'){
					this.citycode=3;
				}
				this.cityHouseList=cityHouseList[this.citycode].houselist;
			},
			getsearch(){
				// console.log(this.iptval);
				// 将搜索记录存进本地
				if(this.searchwords.length==0){
					this.searchwords.push(this.iptval);
				}
				else{
					let temp=-1;
					this.searchwords.forEach((item,index)=>{
						if(this.iptval==item){
							temp=index;
						}
					})
					if(temp!=-1){
						this.searchwords.splice(temp,1);
					}
					this.searchwords.unshift(this.iptval);
					uni.setStorage({
						key:'set_search',
						data:this.searchwords,
						success:function(){
							// console.log('已保存搜索记录');
						}
					})
					// console.log(this.searchwords);
				}
				// 根据输入的值做判断筛选
				let searchList=[];
				let temp=-1;
				// console.log(this.cityHouseList)
				this.cityHouseList.forEach((item,index)=>{
					// console.log(item.house_title);
					// console.log(item.house_title.indexOf(this.iptval))
					//1.判断house_title
					if(item.house_title.indexOf(this.iptval)>-1){
						// console.log('oooo')
						temp=index;
						searchList.push(this.cityHouseList[temp]);
					}
					// 2.判断house_address_desc
					// console.log(item.house_address_desc.indexOf(this.iptval))
					else if(item.house_address_desc.indexOf(this.iptval)>-1){
						// console.log('oooooo')
						temp=index;
						searchList.push(this.cityHouseList[temp]);
					}
					// 3.判断house_desc
					else if(item.house_desc.indexOf(this.iptval)>-1){
						temp=index;
						searchList.push(this.cityHouseList[temp]);
					}
					// 4.判断house_area_desc
					else if(item.house_area_desc.indexOf(this.iptval)>-1){
						temp=index;
						searchList.push(this.cityHouseList[temp]);
					}
					// 5.判断house_tags
					else if(item.house_tags.indexOf(this.iptval)>-1){
						temp=index;
						searchList.push(this.cityHouseList[temp]);
					}
				})
				console.log(searchList);
				this.resultList=searchList;
				if(searchList.length==0){
					this.isNothing=true;
					this.isResult=false;
				}
				else{
					this.isNothing=false;
					this.isResult=true;
				}
				this.iptval='';
			},
			clicktoSearch(text){
				this.iptval=text;
				this.getsearch();
			},
			deleteHistory(){
				this.searchwords=[];
				uni.setStorage({
					key:"set_search",
					data:this.searchwords,
					success() {
						console.log('删除历史搜索成功')
					}
				})
			}
		},
		onLoad() {
			uni.getStorage({
				key:'set_search',
				success: (res) => {
					console.log(res.data);
					this.searchwords=res.data;
				}
			})
		},
		onShow() {
			this.getCity();
			// this.getsearch();
		}
	}
</script>

<style lang="scss">
.container{
	width: 100%;
	color:#464547 ;
	// 头部
	.top{
		width: 100%;
		padding: 50rpx 0 40rpx 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #eeeeee;
		
		.searchbox{
			width:70%;
			margin: 0 auto;
			// margin-left: 30rpx;
			height: 80rpx;
			background-color: #FFFFFF;
			display: flex;
			align-items: center;
			image{
				width: 40rpx;
				height:40rpx;
				margin-left: 20rpx;
			}
			input{
				display: block;
				width: 90%;
				margin-left: 10rpx;
			}
		}
		.city{
			display: block;
			width: 15%;
			text-align: center;
			margin-left: 20rpx;
			color: #464547;
			font-size: 1.1rem;
		}
		.cancle{
			display: block;
			width: 15%;
			text-align: center;
			margin-right: 20rpx;
			color: #464547;
			font-size: 1.1rem;
		}
	}
	// 历史记录
	.title{
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx;
		color:#464547 ;
		font-weight: bold;
		text{
			display: block;
			margin-left: 30rpx;
		}
		image{
			width: 40rpx;
			height: 40rpx;
			display: block;
			margin-right:30rpx ;
		}
	}
	
	// 历史记录
	.historylist{
		margin-left: 30rpx;
		width:90%;
		margin: 0 auto;
		margin-top: 30rpx;
		// border: 1px solid #eeeeee;
		display: flex;
		flex-direction: column;
		align-items: center;
		.item{
			width: 100%;
			padding: 20rpx 0;
			border-bottom: 1px solid #eeeeee;
		}
	}
	// 热门搜索
	.hotlist{
		width: 90%;
		margin: 0 auto;
		margin-top: 30rpx;
		display: flex;
		flex-wrap: wrap;
		.hotitem{
			padding: 0.2rem 0.5rem;
			display: block;
			margin-right: 0.5rem;
			background-color: #Eeeeee;
		}
	}
	// 搜索结果
	.resultList{
		width: 90%;
		margin: 0 auto;
		margin-top: 100rpx;
		.item{
			width: 100%;
			display: flex;
			align-items: center;
			margin-bottom: 15rpx;
			display: flex;
			.pic{
				width: 150rpx;
				height: 150rpx;
				border-radius: 20rpx;
				overflow: hidden;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.content{
				flex: 1;
				margin-left: 10rpx;
				height: 150rpx;
				text{
					display: block;
				}
			}
		}
	}
	.nothing{
		display: block;
		width: 90%;
		margin: 0 auto;
		margin-top: 60rpx;
		text-align: center;
	}
	
}

</style>
